package com.mallcai.bigdata.ladon.vo.args.component;

import com.mallcai.bigdata.ladon.dpl.entity.report.QueryField;
import com.mallcai.bigdata.ladon.dpl.vo.DataType;
import com.mallcai.bigdata.ladon.dpl.vo.QueryFieldType;
import com.mallcai.bigdata.ladon.utils.AnalysisExpressionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by oneape<oneape15@163.com>
 * Created 2019-08-01 20:26.
 * Modify:
 */
public final class ComponentFactory {

    public static BaseComponent build(QueryField field) {
        BaseComponent component;

        boolean isLov = QueryFieldType.contains(field.getType(), QueryFieldType.LOV);
        if (isLov) {
            component = selectBuild(field);
        } else {
            DataType dt = DataType.getByCode(field.getDataType());
            switch (dt) {
                case DATE:
                    component = dateComponent(field);
                    break;
                case DATETIME:
                    component = datetimeComponent(field);
                    break;
                default:
                    component = inputBuild(field);
            }
        }

        // 基础信息
        component.setDefaultValue(AnalysisExpressionUtils.getDefaultValueToHtml(field.getDataType(), field.getValDefault(), true));
        component.setErrMessage(field.getErrMessage());
        component.setPlaceholder(field.getPlaceholder());

        return component;
    }

    // input组件构建
    private static InputComponent inputBuild(QueryField field) {
        InputComponent component = new InputComponent();
        component.setMax(AnalysisExpressionUtils.getDefaultValueToHtml(field.getDataType(), field.getValMax(), false));
        component.setMin(AnalysisExpressionUtils.getDefaultValueToHtml(field.getDataType(), field.getValMin(), false));
        return component;
    }

    // date组件构建
    private static DateComponent dateComponent(QueryField field) {
        DateComponent component = new DateComponent();
        component.setMax(AnalysisExpressionUtils.getDefaultValueToHtml(field.getDataType(), field.getValMax(), false));
        component.setMin(AnalysisExpressionUtils.getDefaultValueToHtml(field.getDataType(), field.getValMin(), false));
        return component;
    }


    // date组件构建
    private static DatetimeComponent datetimeComponent(QueryField field) {
        DatetimeComponent component = new DatetimeComponent();
        component.setMax(AnalysisExpressionUtils.getDefaultValueToHtml(field.getDataType(), field.getValMax(), false));
        component.setMin(AnalysisExpressionUtils.getDefaultValueToHtml(field.getDataType(), field.getValMin(), false));
        return component;
    }

    // Select组件构建
    private static SelectComponent selectBuild(QueryField field) {
        boolean multiple = QueryFieldType.contains(field.getType(), QueryFieldType.MULTIPLE);

        SelectComponent component = new SelectComponent();
        component.setMultiple(multiple ? 1 : 0);

        List<String> dependOns = new ArrayList<>();
        if (StringUtils.isNotBlank(field.getDependOn())) {
            String[] arr = StringUtils.split(field.getDependOn(), ",");
            Collections.addAll(dependOns, arr);
        }
        component.setDependOn(dependOns);
        return component;
    }

}
